package com.tulingxueyuan.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.common.util.ComConstants;
import com.tulingxueyuan.mall.modules.ums.model.UmsMember;
import com.tulingxueyuan.mall.modules.ums.model.UmsMemberReceiveAddress;
import com.tulingxueyuan.mall.modules.ums.service.UmsMemberReceiveAddressService;
import com.tulingxueyuan.mall.modules.ums.service.UmsMemberService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/member/address")
@Api(tags = "memberAddress",description = "地址")
public class MemberAddressController {
    @Autowired
    UmsMemberService memberService;
    @Autowired
    UmsMemberReceiveAddressService receiveAddressService;
//    获取地址
    @GetMapping("/list")
    public CommonResult listAddress(){
        UmsMember umsMember = memberService.getCurrentMember();
        LambdaQueryWrapper<UmsMemberReceiveAddress> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsMemberReceiveAddress::getMemberId,umsMember.getId());
        List<UmsMemberReceiveAddress> list = receiveAddressService.list(lqw);
        return CommonResult.success(list);
    }
//    新增
    @PostMapping("/add")
    public CommonResult addAddress(@RequestBody UmsMemberReceiveAddress receiveAddress){
        UmsMember umsMember = memberService.getCurrentMember();
        receiveAddress.setMemberId(umsMember.getId());
        boolean save = receiveAddressService.save(receiveAddress);
        if (save){
            return CommonResult.success(save);
        }else {
            return CommonResult.failed("保存失败");
        }
    }
//    修改
    @PostMapping("/update/{id}")
    public CommonResult updateAddress(@PathVariable Long id, @RequestBody UmsMemberReceiveAddress receiveAddress){
//        new LambdaQueryWrapper<>();
//        receiveAddressService.u
        receiveAddress.setId(id);
        boolean b = receiveAddressService.updateById(receiveAddress);
        if (b){
            return CommonResult.success(b);
        }else {
            return CommonResult.failed("保存失败");
        }
    }
//    删除
    @PostMapping("/delete/{id}")
    public CommonResult deleteAddress(@PathVariable Long id){
        boolean b = receiveAddressService.removeById(id);
        if (b){
            return CommonResult.success(b);
        }else {
            return CommonResult.failed("保存失败");
        }
    }
}
